import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class AppTextField extends StatefulWidget {
  final TextInputType keyboardType;
  final String hintText;
  final TextEditingController? controller;
  final TextStyle? style;
  final TextStyle? hintStyle;
  final EdgeInsets? contentPadding;
  final int maxLines;
  final int? maxLength;
  final List<TextInputFormatter>? inputFormatter;
  final FocusNode? focusNode;
  final bool autofocus;
  final Function? onChange;
  final Function? onTap;
  final bool obscureText;
  final EdgeInsets? padding;
  final double? minHeight;

  /// 头部widget
  final Widget? prefix;

  /// 尾部widget
  final Widget? suffix;

  /// decoration
  final Color backgroundColor;
  final double borderRadius;
  final BoxBorder? border;
  final TextAlign? textAlign;
  final bool counterTextVisible;
  final bool readOnly;

  AppTextField({
    super.key,
    this.keyboardType = TextInputType.text,
    this.hintText = "",
    this.style,
    this.contentPadding,
    this.maxLines = 1,
    this.inputFormatter,
    this.focusNode,
    this.maxLength,
    this.autofocus = false,
    this.onChange,
    this.prefix,
    this.suffix,
    this.backgroundColor = Colors.transparent,
    this.border,
    this.borderRadius = 0,
    this.obscureText = false,
    this.padding,
    this.textAlign,
    this.minHeight,
    this.counterTextVisible = false,
    this.hintStyle,
    TextEditingController? controller,
    this.readOnly = false,
    this.onTap,
  }) : controller = controller ?? TextEditingController();

  @override
  State<AppTextField> createState() => _AppTextFieldState();
}

class _AppTextFieldState extends State<AppTextField> {
  @override
  void initState() {
    WidgetsBinding.instance.addPostFrameCallback((_) {
      if (widget.controller != null) {
        widget.controller!.addListener(() {
          if (mounted) {
            setState(() {});
          }
        });
      }
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: widget.padding ??
          EdgeInsets.symmetric(vertical: 10.h, horizontal: 15.w),
      constraints: BoxConstraints(minHeight: widget.minHeight ?? 30.h),
      decoration: BoxDecoration(
          color: widget.backgroundColor,
          borderRadius: BorderRadius.circular(widget.borderRadius),
          border: widget.border
        // border: widget.border ?? Border(
        //   bottom: BorderSide(
        //     width: 1.h,
        //     color: Colors.grey.shade200,
        //   )
        // ),
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          if (widget.prefix != null) widget.prefix!,
          Expanded(
            child: TextField(
              controller: widget.controller,
              keyboardType: widget.keyboardType,
              inputFormatters: widget.inputFormatter,
              focusNode: widget.focusNode,
              autofocus: widget.autofocus,
              obscureText: widget.obscureText,
              textAlign: widget.textAlign ?? TextAlign.start,
              readOnly: widget.readOnly,
              onTap: () => widget.onTap?.call(),
              decoration: InputDecoration(
                hintText: widget.hintText,
                hintStyle: widget.hintStyle ??
                    const TextStyle(fontSize: 13, color: Color(0xFFBCBCBC)),
                contentPadding:
                widget.contentPadding ?? const EdgeInsets.only(left: 0),
                border: InputBorder.none,
                filled: true,
                fillColor: Colors.transparent,
                isDense: true,
                counterText: widget.counterTextVisible
                    ? "${widget.controller!.text.length}/${widget.maxLength}"
                    : '',
              ),
              maxLines: widget.maxLines,
              maxLength: widget.maxLength,
              style: widget.style ?? TextStyle(fontSize: 14.sp),
              onChanged: (String input) {
                widget.onChange?.call(input);
              },
            ),
          ),
          if (widget.suffix != null) widget.suffix!
        ],
      ),
    );
  }

}
